<style lang="scss">
  .downblock {
    display: flex;
    justify-content: space-around;
    align-content: center;
    align-items: center;
    flex-direction: column;
    height: 90%;
    width: 30%;
    border: 1px solid rgb(230, 230, 230);
    border-radius: 1vw;
    padding: 1vw;
  }

  .binner {
    display: flex;
    justify-content: space-around;
    align-content: center;
    align-items: center;
    flex-direction: column;
  }

  .login-body-content {
    padding: 6px 0 0;
    height: 100%;
    padding: 4px 0;
    overflow: auto;
  }

  .login-content-item {
    margin: 0;
    line-height: normal;
    padding: 7px 16px;
    clear: both;
    list-style: none;
    font-size: 13px;
  }

  .login-body-content:hover {
    background-color: #97c0ef;
    cursor: pointer;
  }
</style>

<template>
  <div v-if="showModal">
    <Modal v-model="newValue" :mask-closable="false" :footer-hide="true" width="360" @on-cancel="newValue=false">
      <p slot="header" style="text-align:center">
        <Icon type="ios-information-circle" style="color:#f60"></Icon>
        <span>请选择组织</span>
      </p>
      <div style="align:center">
        <template v-for="(item, index) in structList">
          <ul class="login-body-content" :key="index" @click="toMainPage(item,page)">
            <div style="float:left;">
              <li class="login-content-item">
                <span style="font-size: 14px;">{{item.gName}}</span>
              </li>
            </div>
          </ul>
        </template>
      </div>
    </Modal>
  </div>
</template>

<script>
  import ajax from "../ajax_util"
  import * as types from '../store/types'
  export default {
    name: "changeApartment",
    data() {
      return {
        structList: [],
        checkmodal: this.datamodal,
        showModal: false
      }
    },
    created: function() {
      console.log("this.$store.state.user-----》", this.$store.state.user, this.newValue)
      if (this.$store.state.token != undefined && this.$store.state.token != null) {
        if (this.$store.state.user == undefined || this.$store.state.user == null || this.$store.state.user
          .department == undefined || this.$store.state.user.department == null) {
          console.log("----------------------------》》》》》")
          this.getPerm()
        } else if (this.$store.state.user.structs.length == 1) {
          console.log("--------------s--------------》》》》》", this.$store.state.user.department)
          this.$store.state.user.department = this.$store.state.user.structs[0].gName
        } else if (this.$store.state.user.structs.length > 1) {
          this.structList = this.$store.state.user.structs
          this.showModal = true
        }
      }
    },
    methods: {
      test() {
        console.log("00000");
      },
      clickAction(value) {
        console.log('clickAction', value)
        this.$emit('click', value);
      },
      toMainPage(department, page) {
        //console.log("-----page----",page);
        this.updateRedis(department, page);
      },
      updateRedis(struct, page) {
        var that = this;
        var url = this.CONFIG.url_know + '/updateRedis';
        ajax.post({
          url: url,
          that: that,
          data: struct,
          success: function(data) {
            that.$store.commit(types.LOGIN, data.token);
            var cookieText = "Authorization=" + data.token + "; path=/"
            document.cookie = cookieText;
            that.$store.state.user.department = struct.gName;
            localStorage.user = JSON.stringify(that.$store.state.user)
            if (page === "login") {
              that.$router.push({
                path: '/processmain'
              });
              localStorage.menu = "/processmain";
              that.$Notice.success({
                title: '登录成功'
              });
              that.editMessage();
            } else if (page === "changeStruct") {
              that.$Message.success('切换组织成功!');
            }
            that.newValue = false;
            console.log("struct--------->", struct, that.$store.state.user.department)
          }
        })
      },
      editMessage() {
        console.log("修改个人信息", this.utils.listIsEmpty(this.$store.state.user.tenants), this.$store.state.user
        .department);
        console.log(this.$store.state.user, window.location.hash, document.domain);
        if (this.$store.state.user.fourA == "" || this.$store.state.user.fourA == null || this.$store.state.user
          .id_card == "" || this.$store.state.user.id_card == null) {
          this.$Modal.warning({
            title: '温馨提示：',
            content: "您的个人信息尚未填写完整，请立即前往修改！",
            width: '500',
            onOk: () => {
              this.$router.push({
                path: '/personalCenter'
              });
              localStorage.menu = '/personalCenter'
              console.log(window.location.hash);
            }
          });
        } else if (this.utils.listIsEmpty(this.$store.state.user.tenants) && (this.$store.state.user.department ==
            undefined || this.$store.state.user.department == null || this.$store.state.user.department == '')) {
          this.$Modal.warning({
            title: '温馨提示：',
            content: "组织信息不完整，请联系相关管理员补充，否则会影响部分工单申请！",
            width: '500',
            onOk: () => {

            }
          });
        }
      },
      getPerm() {
        var that = this;
        var url = this.CONFIG.url_know + '/perm';
        ajax.get({
          url: url,
          that: that,
          success: function(data) {
            that.$store.commit(types.PERM, JSON.stringify(data));
            let userPermission = data;
            that.$store.dispatch('GenerateRoutes', userPermission).then(() => {
              //生成该用户的新路由json操作完毕之后,调用vue-router的动态新增路由方法,将新路由添加
              that.$router.addRoutes(that.$store.state.addRouters)
            })
            that.getUser();
          }
        })
      },
      getUser() {
        var that = this;
        var url = this.CONFIG.url_know + '/user';
        ajax.get({
          url: url,
          that: that,
          success: function(data) {
            if (data.structs.length == 1) {
              that.toMainPage(data.structs[0], "login");
            } else if (data.structs.length > 1) {
              that.structList = data.structs
              that.showModal = true
            } else {
              var department = {};
              department.id = 0;
              department.gName = "";
              that.toMainPage(department, "login")
            }
            var obj = JSON.stringify(data);
            that.$store.commit(types.USER, obj);
            that.newValue = true
            console.log("that.newValue ----->", that.newValue)
            // that.geGuidance(data.fourA)
          }
        })
      },
      /**
       * 获取引导列表
       */
      geGuidance(userId){
        var that = this;
        var url =  'http://localhost:8234/geGuidanceList/' + userId;
        ajax.get({
          url: url,
          that: that,
          success: function(data) {
            console.log("data = ", data)
            sessionStorage.guidanceList = JSON.stringify(data)
          }
        })
      },
    },

    computed: {
      newValue: {
        get: function() {
          return this.value;
        },
        set: function(value) {
          this.$emit('input', value);
        }
      },
    },
    props: {
      dataList: {
        type: Array,
        default: []
      },
      datamodal: {
        type: Boolean,
        default: false,
      },
      value: {
        dafault: false
      },
      page: {}
    },
    watch: {
      dataList(newV, oldV) {
        //console.log("dataList:newV,oldV------------------",newV,oldV)
        // this.structList = newV
      },
      datamodal(newV, oldV) {
        //console.log("wwww------------------",newV,oldV)
        this.checkmodal = newV
      }
    }
  }
</script>
